<template>
	<view class="container">
		<view class="main-wrap" v-if="list.length > 0">
			<view class="item column" v-for="(item, index) in list" :key="index" @click="chooseAddress(item)">
				<view class="row">
					<text class="name">{{ item.pickupName }}</text>
					<text class="mobile">{{ item.pickupMobile }}</text>
				</view>
				<text class="content">{{ item.province }} {{ item.city}}{{ item.district }} {{ item.addressDet}}</text>
				<view class="bot row b-t" @click.stop.prevent="stopPrevent">
					<view class="default-address" @click.stop.prevent="setDefaultAddress(item)">
						<text class="iconfont iconxuanzhong"></text>
						<text>选择地址</text>
					</view>
					<view class="btn center" @tap="showDelModal(item)">
						<text class="iconfont iconshanchu"></text>
						<text>删除</text>
					</view>
					<view class="btn center" @tap="editAddress('update',item)">
						<text class="iconfont iconbianji1"></text>
						<text>编辑</text>
					</view>
				</view>
			</view>
			<view class="bot-btn-wrap fix-bot bg-none">
				<mix-button text="新增收货地址" icon="icon-jia2" :iconSize="36" :isConfirm="false"
					@onConfirm="editAddress('add')"></mix-button>
			</view>
		</view>
		<tui-modal :show="modalShow" @click="handleClick" @cancel="hide" title="提示" :content="modalContent"></tui-modal>
		<loading v-if="isLoading"></loading>
		<mix-empty v-else-if="!isLoading && list.length === 0" :type="'address'" :backgroundColor="'#fff'"></mix-empty>
		<!-- <uni-popup ref="addressPopup"   type="center">
		<view class="address-box" >
			 <text class="title">请确认您的收货地址</text>
			 <text>收货人：{{currenAddress.pickupName}}</text>
			 <text>联系方式：{{currenAddress.pickupMobile}}</text>
			 <text>收货地址：{{ currenAddress.province }} {{ currenAddress.city}}{{ currenAddress.district }} {{ currenAddress.addressDet}}</text>
			<view class="btns">
				<text class="btn" @click="makeSure">确认</text>
				<text class="btn" @click="$refs.addressPopup.close()">取消</text>
			</view>
		</view>
	</uni-popup> -->

		<tui-modal :custom="true" :show="addressVisible" title="提示">
			<view class="login-modal-title">请确认您的收货地址</view>
			<view class="login-modal-content address-box">
				<text>收货人：{{currenAddress.pickupName}}</text>
				<text>联系方式：{{currenAddress.pickupMobile}}</text>
				<text>收货地址：{{ currenAddress.province }} {{ currenAddress.city}}{{ currenAddress.district }}
					{{ currenAddress.addressDet}}</text>
			</view>
			<view class="tui-modalBtn-box">
				<button class="login-modal-btn login-sure-btn " @tap="makeSure">确认</button>
				<button class="login-modal-btn login-cancel-btn" @tap="addressVisible=false">取消</button>
			</view>
		</tui-modal>
		<tui-modal></tui-modal>
		<loading></loading>
	</view>
</template>

<script>
	import mixButton from '@/components/mix-button/mix-button.vue'
	import mixEmpty from '@/components/mix-empty/mix-empty.vue'
	// import mixin from "@/utils/mixin.js"

	export default {
		// mixins: [mixin],
		components: {
			mixEmpty,
			mixButton
		},
		data() {
			return {
				prizeId: '',
				currenAddress: {},
				sureVisible: false,
				addressVisible: false,
				isLoading: false,
				list: [],
				modalContent: "确认要删除地址吗？",
				modalShow: false,
				curIndex: 0,
				delItem: null,
				submitOrderFlag: '',
			}
		},
		onLoad(options) {
			// this.list=JSON.parse(options.addressData);
			this.getAddress()
			if (options.submitOrderFlag == 1) {
				this.submitOrderFlag = options.submitOrderFlag
			}
			this.prizeId = options.prizeId
		},
		methods: {
			getAddress() { //获取用户信息
				this.isLoading = true;
				let params={
					type:2,
				}
				this.$http.customersIncipient(params).then((res) => {
					this.isLoading = false;
					if (res.status === 0) {
						if (res.data) {
							this.list = res.data.customersAddressData;
						}
					}
				})
			},
			showDelModal(item) { //确认删除弹框
				this.modalShow = true;
				this.delItem = item;
			},
			handleClick(e) {
				let index = e.index;
				if (index === 1) {
					let id = this.delItem.customerAddressId;
					this.$http.deleteAddress(id).then((res) => {
						if (res.status === 0) {
							this.hide();
							this.getAddress();
							setTimeout(function() {
								uni.showToast({
									title: "删除成功!",
									icon: "none",
									duration: 500
								});
							}, 200)
						}
					})
				}
				this.hide();
			},
			hide() {
				this.modalShow = false;
			},
			editAddress(type, item) {
				if (type === 'update') {
					this.navTo(`/packageA/pages/editAddress/editAddress?id=${item.customerAddressId}`);
				} else {
					this.navTo('/packageA/pages/editAddress/editAddress');
				}

			},
			makeSure() {
				let currenAddress = this.currenAddress
				let params = {
					id: this.prizeId,
					receiveName: currenAddress.pickupName,
					receiveTel: currenAddress.pickupMobile,
					address: currenAddress.province + currenAddress.city + currenAddress.district + currenAddress
						.addressDet
				}
				this.$http.addPrizeAddress(params).then(res => {
					console.log(res)
					if (res.status === 0) {
						uni.reLaunch({
							url: '../../../pages/home/home?reload=true'
							// url: '../home/home?reload=true'
						})
					} else {
						uni.showToast({
							icon: "none",
							title: "地址提交失败"
						})
					}
				})
				// var pages = getCurrentPages();
				// var currPage = pages[pages.length - 1]; //当前页面
				// var prevPage = pages[pages.length - 2]; //上一个页面
				// //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
				// console.log("currPage",currPage,"prevPage",prevPage,prevPage.hasOwnProperty('setData'))
				// if(prevPage,prevPage.hasOwnProperty('setData')){
				// 	prevPage.setData({
				// 			address:this.currenAddress
				// 	})
				// }else{
				// 	prevPage.address= this.currenAddress
				// }

				//  uni.navigateBack(); //返回上一个页面	
			},
			setDefaultAddress(item) { //选中地址
				this.currenAddress = item
				// this.$refs.addressPopup.open()
				this.addressVisible = true

				// var pages = getCurrentPages();
				// var currPage = pages[pages.length - 1]; //当前页面
				// var prevPage = pages[pages.length - 2]; //上一个页面
				// //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
				// prevPage.setData({
				// 		address:item
				// })
				//  uni.navigateBack(); //返回上一个页面	


				// if(item.isDefault===1){
				// 	uni.showToast({
				// 		title: "已经是默认地址了!",
				// 		icon: "none",
				// 		duration: 500
				// 	});
				// 	return;
				// }
				// item.isDefault=1;
				// let params=item;
				// params.ifEdit="default";
				// this.$http.updateAddress(params).then((res) => {
				// 	if(res.status === 0) {

				// 		this.getAddress();
				// 		setTimeout(function(){
				// 			uni.showToast({
				// 				title: "默认地址设置成功!",
				// 				icon: "none",
				// 				duration: 500
				// 			});
				// 		},200)
				// 	}
				// })

			},
			//选择地址
			chooseAddress(item) {
				// if(!this.isChoose){
				// 	return;
				// }
				// this.$util.prePage().setAddress(item);
				// uni.navigateBack();
				if (this.submitOrderFlag == 1) {
					// this.$util.prePage().getAddress();
					setTimeout(() => {
						uni.navigateTo({
							url: '../submitOrder/submitOrder?addressId=' + item.customerAddressId
						});
					}, 500)
					// uni.redirectTo({
					// 	url:'../submitOrder/submitOrder'
					// })
				}
			},
			stopPrevent() {},
		}
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style scoped lang="scss">
	.login-modal-title {
		text-align: center;
		font-size: 34rpx;
		color: #111;
		padding-top: 10rpx;
		font-weight: bold;
	}

	.login-modal-content {
		text-align: justify;
		color: #999;
		font-size: 28rpx;
		padding-top: 20rpx;
		padding-bottom: 60rpx;
		max-height: 70vh;
		overflow-y: auto;
	}

	.tui-modalBtn-box {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between
	}

	.login-modal-btn {
		width: 240rpx;
		height: 80rpx;
		line-height: 80rpx;
		position: relative;
		border-radius: 50px;
		font-size: 30rpx;
		overflow: visible;
		margin: 0 12rpx;
	}

	.login-link {
		text-decoration: underline;
		color: #EA655E;
	}

	.login-cancel-btn {
		color: #999999;
		background: #F5F5F5 !important;
	}

	.login-sure-btn {
		color: #fff;
		background: linear-gradient(151deg, #F69E97 0%, #EA655E 100%);
	}

	.address-box {
		display: flex;
		flex-direction: column;
		justify-content: start;
		align-items: flex-start;
		width: 60vw;

		>view,
		>text {
			padding: 30rpx 10rpx;
		}

		.title {
			align-self: center;
		}

		.btns {
			align-self: stretch;
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			align-items: center;

			>text {
				padding: 10rpx 50rpx;
				background-color: #ff0000;
				color: #ffffff;
				font-size: 26rpx;
				border-radius: 10rpx;

			}

		}
	}

	.container {
		padding-top: 20rpx;
		padding-bottom: 130rpx;
	}

	.main-wrap {
		// opacity: 0;
		transition: opacity .2s;

		&.show {
			opacity: 1;
		}
	}

	.item {
		padding: 30rpx 28rpx 0;
		margin: 0 20rpx 20rpx;
		background-color: #fff;
		border-radius: 12rpx;
		box-shadow: 0rpx 0rpx 20rpx rgba(0, 0, 0, .03);

		.name {
			margin-right: 28rpx;
			font-size: 32rpx;
			color: #333;
		}

		.mobile {
			margin-right: 32rpx;
			font-size: 28rpx;
			color: #999;
		}

		.tag {
			padding: 0 8rpx;
			font-size: 20rpx;
			color: #fff;
			line-height: 28rpx;
			// background-color: $base-color;
			border-radius: 2rpx;
		}

		.content {
			width: 560rpx;
			min-height: 70rpx;
			margin: 28rpx 0 20rpx;
			font-size: 28rpx;
			color: #999;
			line-height: 1.44;
		}

		.bot {
			justify-content: flex-end;
			height: 79rpx;
			position: relative;

			&:before {
				left: -26rpx;
				right: -26rpx;
				border-color: #f0f0f0;
			}
		}

		.default-address {
			position: absolute;
			left: 0rpx;
			font-size: 26rpx;

			.iconfont {
				padding: 14rpx 0 14rpx 14rpx;
				color: #ccc;

				&.active {
					color: $themeColor1;
				}
			}
		}

		.btn {
			margin-left: 15rpx;
			font-size: 26rpx;
			color: #444;

			.iconfont {
				margin-right: 8rpx;
				font-size: 34rpx;
			}
		}
	}
</style>
